package com.developmentReformProject.controller;

import com.alibaba.fastjson.JSON;
import com.developmentReformProject.dao.ApplyDao;
import com.developmentReformProject.dto.AdminQueryDto;
import com.developmentReformProject.dto.ApplyQueryDto;
import com.developmentReformProject.entity.Admin;
import com.developmentReformProject.entity.Apply;
import com.developmentReformProject.service.AuditProjectLibraryService;
import com.developmentReformProject.utils.FileUtil;
import com.developmentReformProject.utils.PageUtil;
import com.developmentReformProject.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    ApplyDao applyDao;
    @Autowired
    AuditProjectLibraryService auditProjectLibraryService;
    @GetMapping("/queryList")
    public ResultVo list(ApplyQueryDto applyQueryDto) throws Exception{
        //调用业务
        PageUtil<Apply> data= auditProjectLibraryService.pageList(applyQueryDto);
        //响应
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @PostMapping("/delete")
    public ResultVo deleteEnterpriseAdmin(int projectNo)throws Exception{
        applyDao.deleteProject(projectNo);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @PostMapping("/update")
    public ResultVo updateEnterpriseAdmin(Apply apply)throws Exception{
        applyDao.updateApply(apply);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @PostMapping("/addProject")

    public ResultVo addProject(MultipartFile file, HttpServletRequest request, String projectAddress, String applyName,
                               String projectType, String specificType, Double investment,
                               Double revenue,@DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,@DateTimeFormat(pattern = "yyyy-MM-dd") Date lifeCycle,
                               String paper,Integer enterpriseId) throws Exception{
        //Apply apply=new Apply(applyName,projectType,investment,revenue,startTime,lifeCycle,specificType,projectAddress,enterpriseId);
        Apply apply=new Apply();
        apply.setApplyName(applyName);
        apply.setProjectAddress(projectAddress);
        apply.setProjectType(projectType);
        apply.setRevenue(revenue);
        apply.setSpecificType(specificType);
        apply.setStartTime(startTime);
        apply.setInvestment(investment);
        apply.setEnterpriseId(enterpriseId);
        apply.setStartTime(startTime);
        apply.setLifeCycle(lifeCycle);
        String fileName=null;
        System.out.println(apply.getApplyName());
        if(file!=null){
            fileName= FileUtil.upload(file,request);//文件上传
            System.out.println(fileName);
            ResultVo resultVo=new ResultVo(fileName);
            return resultVo;
        }
            apply.setProjectAssignmentPaper(paper);
            applyDao.addApply(apply);
            ResultVo resultVo = new ResultVo();
            return resultVo;
    }

    @GetMapping("/download")
    public void download( HttpServletResponse resp,int projectNo) throws Exception {
        Apply apply= applyDao.queryByNo(projectNo);
        String filename=apply.getProjectAssignmentPaper();
        FileUtil.download(filename, resp);//下载数据
    }
    @GetMapping("/listDelete")
    public ResultVo listDelete(ApplyQueryDto applyQueryDto) throws Exception {
        PageUtil<Apply> data =auditProjectLibraryService.cleanAdmin(applyQueryDto);
        ResultVo resultVo=new ResultVo<>(data);
        return  resultVo;
    }
    @PostMapping("/reduction")
    public ResultVo reduction(int projectNo) throws Exception{
        Apply apply=new Apply();
        apply.setProjectNo(projectNo);
        apply.setProjectStateId(1);
        applyDao.updateApply(apply);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @PostMapping("/cleanApply")
    public ResultVo cleanAdmin(int projectNo)throws Exception{
        applyDao.cleanProject(projectNo);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @GetMapping("/count")
    public String count() {
        List<Integer> list=new ArrayList();
        list.add(applyDao.countOne());
        list.add(applyDao.countTwo());
        list.add(applyDao.countThree());
        list.add(applyDao.countFour());
        list.add(applyDao.countFive());
        list.add(applyDao.countSix());
        System.out.println(JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }

}
